import 'dotenv/config';
import { NestFactory } from '@nestjs/core';
import {
  FastifyAdapter,
  NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { AppModule } from '@modules/app.module';
import { useCors } from '@/configs';
import { useGlobalFilters } from '@/filters';
import { useGlobalInterceptors } from './interceptors';
import fastifyCookie, { FastifyCookieOptions } from '@fastify/cookie';
import { contentParser } from "fastify-multer";
import { COOKIE_SECRET } from './constants/secret';
import { utilities, WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import 'winston-daily-rotate-file';
import { Env } from './enums';
async function bootstrap() {
  const logger = WinstonModule.createLogger({
    transports: [
      new winston.transports.DailyRotateFile({
        dirname: `logs`, // 日志保存的目录
        filename: '%DATE%.log', // 日志名称，占位符 %DATE% 取值为 datePattern 值。
        datePattern: 'YYYY-MM-DD', // 日志轮换的频率，此处表示每天。
        zippedArchive: true, // 是否通过压缩的方式归档被轮换的日志文件。
        maxSize: '20m', // 设置日志文件的最大大小，m 表示 mb 。
        maxFiles: '14d', // 保留日志文件的最大天数，此处表示自动删除超过 14 天的日志文件。
        // 记录时添加时间戳信息
        format: winston.format.combine(
          // winston.format.label({ label: 'right meow!' }),
          winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
          winston.format.json(),
        ),
      }),
      new winston.transports.Console({
        format: winston.format.combine(
          winston.format.timestamp(),
          winston.format.ms(),
          utilities.format.nestLike('NestServe', {
            colors: true,
            prettyPrint: true,
            processId: true,
            appName: true,
          }),
        ),
      }),
    ],
  });
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
    {
      logger,
    },
  );

  if (process.env.NODE_ENV === Env.dev) {
    app.setGlobalPrefix('api');
  }

  useCors(app);
  useGlobalFilters(app, logger);
  useGlobalInterceptors(app);

  app.register(fastifyCookie, {
    secret: COOKIE_SECRET,
  } as FastifyCookieOptions);

  app.register(contentParser);

  await app.listen(3000, '0.0.0.0', (err, address) => {
    console.log(`Server is running on ${address}`);
  });
}
bootstrap();
